package com.weds.edwx.web;

import com.weds.core.annotation.Logs;
import com.weds.edwx.entity.RequestEntity;
import com.weds.edwx.entity.ReturnEntity;
import com.weds.edwx.service.BusinessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping(value = "/checkinout")
@Api(value = "获取考勤记录", description = "获取考勤记录")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    private static final String FULL = "-1";
    private static final String SUCCESS = "0";

    @Logs
    @ApiOperation(value = "获取考勤记录",notes = "获取考勤记录")
    @RequestMapping(value ="/getCheckinout",method = RequestMethod.GET)
    public Map<String,Object> getCheckinout(@RequestParam(required = false) String start , @RequestParam(required = false) String end ,
                                            @RequestParam(required = false) String pin , @RequestParam(required = false) String sn ,
                                            @RequestParam(required = false) String param1 , @RequestParam(required = false) String param2){
        Map<String,Object> map = new HashMap<String,Object>();
        RequestEntity requestEntity = new RequestEntity();
        requestEntity.setRequestDate(new Date());
        try{
            if (param1 == "" || param1 == null){
                requestEntity.setParam1("1");
            }
            if (param2 == "" || param2 == null){
                requestEntity.setParam2("2000");
            }
            requestEntity.setStart(start);
            requestEntity.setEnd(end);
            requestEntity.setPin(pin);
            requestEntity.setSn(sn);
            List<ReturnEntity> list = new ArrayList<ReturnEntity>();
            int totalCount = businessService.selectCountAttence(requestEntity);
            if (totalCount > 0) {
                list = businessService.selectAttence(requestEntity);
            }
            requestEntity.setRet(SUCCESS);
            requestEntity.setTotalCount(totalCount);
            businessService.insertLog(requestEntity);
            map.put("totalCount",totalCount);
            map.put("option","GetCheckinout");
            map.put("ret",SUCCESS);
            map.put("records",list);
            return map;
        }catch (Exception e){
            e.printStackTrace();
            requestEntity.setRet(FULL);
            businessService.insertLog(requestEntity);
            map.put("option","GetCheckinout");
            map.put("ret",FULL);
            return map;
        }

    }
}
